package com.example.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class MyRealm extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        获取身份信息
        String principal = authenticationToken.getPrincipal().toString();
//        获取凭证信息
        String password = new String((char[])authenticationToken.getCredentials());
        System.out.println("身份信息：" + principal + "凭证信息：" + password);
//        获取数据库中存储的用户信息
        if ("zhangsan".equals(principal)) {
//            数据库中存储的加盐三次迭代的密码
            String realPass = "7174f64b13022acd3c56e2781e098a5f";
//            创建封装校验参数逻辑对象，封装数据返回
            AuthenticationInfo info = new SimpleAuthenticationInfo(
                    authenticationToken.getPrincipal(),
                    realPass,
                    ByteSource.Util.bytes("salt"),
                    principal
            );
            return info;
        } else {
            return null;
        }

    }
}
